package com.ruoyi.system.service.zhaungxiuservice.shopService.impl;

import com.ruoyi.common.config.TenpayConfig;
import com.ruoyi.common.core.domain.entity.ProductInfow;
import com.ruoyi.common.utils.ConstantInfo;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.TenPayUtils;
import com.ruoyi.common.utils.uuid.Convert;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderMaster;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.ProductInfo;
import com.ruoyi.system.domain.zhuangxiumain.shopMain.SupplierInfo;
import com.ruoyi.system.mapper.zhuangxiumapper.orderformMapper.OrderMasterMapper;
import com.ruoyi.system.mapper.zhuangxiumapper.shopMapper.ProductInfoMapper;
import com.ruoyi.system.mapper.zhuangxiumapper.shopMapper.SupplierInfoMapper;
import com.ruoyi.system.service.zhaungxiuservice.shopService.OrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.util.*;

@Service
public class OrderMasterServiceImpl implements OrderMasterService {

    @Autowired(required = false)
    private OrderMasterMapper orderMasterMapper;

    @Autowired(required = false)
    private SupplierInfoMapper supplierInfoMapper;

    @Autowired(required = false)
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private TenpayConfig tenpayConfig;

//    @Override
//    public Map<String, String> payRequest(String payType, String outTradeNo, BigDecimal totalAmount) throws Exception {
//        Map<String, String> map = new HashMap<String, String>();
//        switch (PayTypeEnum.valueOf(payType)) {
//            case TENPAY://财付通
//                SortedMap<String, Object> paramsMap = new TreeMap<String, Object>();
//                //公众账号ID
//                paramsMap.put("appid", tenpayConfig.getAppId());
//                //商户号
//                paramsMap.put("mch_id", tenpayConfig.getMchId());
//                //随机字符串
//                paramsMap.put("nonce_str", Convert.getUUID());
//                //描述
//                paramsMap.put("body", "名绘优家订单支付");
//                //商户订单号(支付编号)
//                paramsMap.put("out_trade_no", outTradeNo);
//                //支付金额,金额单位为 分
//                double price = totalAmount.doubleValue();
//                int totalFee = (int) (price * 100);
//                paramsMap.put("total_fee", String.valueOf(totalFee));
//                //回调地址
//                paramsMap.put("notify_url", ConstantInfo.TENPAY_ORDER_CALLBACK);
//                //交易类型
//                paramsMap.put("trade_type", "APP");
//                //用户端ip
//                String spbillCreateIp = "";
//                InetAddress inetAddress = InetAddress.getLocalHost();
//                if (inetAddress != null) {
//                    spbillCreateIp = inetAddress.getHostAddress();
//                }
//                paramsMap.put("spbill_create_ip", spbillCreateIp);
//                TenPayUtils tenPayUtils = new TenPayUtils();
//                //sign签名
//                String sign = tenPayUtils.createSign(paramsMap, "UTF-8");
//                paramsMap.put("sign", sign);
//                //请求报文
//                String requestXml = TenPayUtils.tenPayXmlInfo(paramsMap);
//                //logger.info("微信支付请求报文: " + requestXml);
//
//                //发送微信支付post请求
//                String tenPayPost = TenPayUtils.httpsRequest(tenpayConfig.getPayUrl(), "POST", requestXml);
//                //获取返回
//                Map<String, String> tenPayMap = (Map<String, String>) TenPayUtils.readXml(tenPayPost, "xml", "");
//                //微信返回状态码
//                if (!tenPayMap.get("return_code").equals("SUCCESS")) {
//                    logger.error("微信支付请求连接失败: " + tenPayMap.get("return_msg"));
//                    throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
//                }
//                //业务结果
//                if (!tenPayMap.get("result_code").equals("SUCCESS")) {
//                    logger.error("err_code: " + tenPayMap.get("err_code"), "err_code_des: " + tenPayMap.get("err_code_des"));
//                    throw new RRException(AppWSConstant.RS_MSG_TENPAY_FALL);
//                }
//                //APPID
//                map.put("appid", tenPayMap.get("appid"));
//                //商户号
//                map.put("partnerid", tenPayMap.get("mch_id"));
//                //预支付交易会话ID
//                map.put("prepayid", tenPayMap.get("prepay_id"));
//                //扩展字段
//                map.put("package", tenpayConfig.getPackageValue());
//                //随机字符串
//                map.put("noncestr", tenPayMap.get("nonce_str"));
//                //时间戳
//                map.put("timestamp", String.valueOf(new Date().getTime()).substring(0, 10));
//                SortedMap<String, Object> signMap = new TreeMap<>(map);
//                String newSign = tenPayUtils.createSign(signMap, "UTF-8");
//                //签名
//                map.put("sign", newSign);
//                break;
//            default:
//                break;
//        }
//        return map;
//    }



    @Override
    public List<OrderMaster> findAllListSupe(OrderMaster orderMaster) {
        return orderMasterMapper.findAllListSupe(orderMaster);
    }

    @Override
    public OrderMaster findAllById(Integer productid) {
        OrderMaster orderMaster = new OrderMaster();
        orderMaster.setProductId(productid);
        return orderMasterMapper.findAllById(productid);
    }

    @Override
    public List<OrderMaster> findAllList(OrderMaster orderMaster) {
        return orderMasterMapper.findAllList(orderMaster);
    }



    // 以下是 店铺中的订单用的*******************************
    @Override
    public List<OrderMaster> selectOrderDingDanList(OrderMaster orderMaster) {
        return orderMasterMapper.selectOrderDingDanList(orderMaster);
    }

    @Override
    public OrderMaster selectOrderDingDanById(Integer orderId) {
        return orderMasterMapper.selectOrderDingDanById(orderId);
    }

    @Override
    public List<OrderMaster> findAllListVoYI(OrderMaster orderMaster) {
        return orderMasterMapper.findAllListVoYI(orderMaster);
    }

    @Override
    public int updateFaHuo(OrderMaster orderMaster) {
        return orderMasterMapper.updateFaHuo(orderMaster);
    }

    @Override
    public int updatepintuan(OrderMaster orderMaster) {
        return orderMasterMapper.updatepintuan(orderMaster);
    }

    @Override
    public OrderMaster selectById(Integer orderId) {
        return orderMasterMapper.selectOrderMasterById(orderId);
    }

    @Override
    public int updateFaHuos(Integer[] orderId) {
        return orderMasterMapper.updateFaHuos(orderId);
    }


    // 总权限的订单管理 以下****************************


    @Override
    public OrderMaster selectOrderMasterById(Integer orderId)
    {
        return orderMasterMapper.selectOrderMasterById(orderId);
    }

    @Override
    public List<OrderMaster> selectOrderMasterList(OrderMaster orderMaster)
    {
        return orderMasterMapper.selectOrderMasterList(orderMaster);
    }

    @Override
    public int insertOrderMaster(OrderMaster orderMaster)
    {
//        orderMaster.setCreateTime(DateUtils.getNowDate());
        return orderMasterMapper.insertOrderMaster(orderMaster);
    }


    @Override
    public int updateOrderMaster(OrderMaster orderMaster)
    {
        return orderMasterMapper.updateOrderMaster(orderMaster);
    }


    @Override
    public int deleteOrderMasterByIds(Integer[] orderIds)
    {
        return orderMasterMapper.deleteOrderMasterByIds(orderIds);
    }


    @Override
    public int deleteOrderMasterById(Integer orderId)
    {
        return orderMasterMapper.deleteOrderMasterById(orderId);
    }

    @Override
    public List<SupplierInfo> selectSupplierInfoList(SupplierInfo supplierInfo) {
        return supplierInfoMapper.selectSupplierInfoList(supplierInfo);
    }

    @Override
    public ProductInfo selectProductInfoById(Integer productId) {
        return productInfoMapper.selectProductInfoById(productId);
    }

    @Override
    public List<ProductInfo> selectProductInfoList(ProductInfo productInfo) {
        return productInfoMapper.selectProductInfoList(productInfo);
    }

    @Override
    public List<OrderMaster> superOrderMasterList(OrderMaster orderMaster) {
        return orderMasterMapper.superOrderMasterList(orderMaster);
    }

    @Override
    public List<OrderMaster> findFenZhan(OrderMaster orderMaster) {
        return orderMasterMapper.findFenZhan(orderMaster);
    }

    @Override
    public List<OrderMaster> ordercustomerIdList(Integer customerId) {
        return orderMasterMapper.ordercustomerIdList(customerId);
    }


//*******************************


//    @Override
//    public List<SupplierInfo> selectSuppTypeAll() {
//        return supplierInfoMapper.selectSuppTypeAll();
//    }
//
//    @Override
//    public SupplierInfo selectSuppTypeById(Integer supplierId) {
//        return supplierInfoMapper.selectSuppTypeById(supplierId);
//    }
//    /**
//     * 根据供应商查询字典数据
//     * @param supplierName
//     * @return 供应商数据集合信息
//     */
//    @Override
//    public SupplierInfo selectSuppTypeByType(String supplierName) {
//        return supplierInfoMapper.selectSuppTypeByType(supplierName);
//    }
//
//    @Override
//    public SupplierInfo checkSuppTypeUnique(String supplierName) {
//        return null;
//    }
//
//    @Override
//    public List<ProductInfow> selectDictDataByType(String productName) {
//        List<ProductInfow> prodDatas = DictUtils.getProduCache(productName);
//        if (StringUtils.isEmpty(prodDatas)){
//            return prodDatas;
//        }
//        prodDatas= productInfoMapper.selectProductByType(productName);
//        if (StringUtils.isEmpty(prodDatas)){
//            DictUtils.setProudCache(productName,prodDatas);
//            return prodDatas;
//        }
//
//        return null;
//    }

    /**
     * 查询线下支付的订单
     * @param orderMaster
     * @return
     */
    public List<OrderMaster> selectOfflineOrderMaster(OrderMaster orderMaster){
        return orderMasterMapper.selectOfflineOrderMaster(orderMaster);
    }

}
